package service

import (
	"context"
	"errors"
	"example.com/m/v2/internal/domain/account_aggregate/entity"
	"example.com/m/v2/internal/domain/account_aggregate/service"
	"log"
)

type AccountAppService struct {
	accountDomainService *service.AccountDomainService
}

func NewAccountAppService(accountDomainService *service.AccountDomainService) *AccountAppService {
	log.Printf("account app service constructed")
	return &AccountAppService{
		accountDomainService: accountDomainService,
	}
}

func (appService *AccountAppService) FindMainAccount(ctx context.Context, mainAccountId uint) (*entity.MainAccountEntity, error) {
	return appService.accountDomainService.FindMainAccount(ctx, mainAccountId)
}

func (appService *AccountAppService) CreateSubAccount(ctx context.Context, mainAccountId uint, appId uint) (*entity.SubAccountEntity, error) {
	// TODO:参数检查
	if mainAccountId == 0 || appId == 0 {
		return nil, errors.New("params error")
	}

	// TODO:判断主账号是否存在
	_, err := appService.accountDomainService.FindMainAccount(ctx, mainAccountId)
	if err != nil {
		return nil, err
	}

	// TODO:判断app是否存在

	// TODO:创建子账号
	subAccountEntity, err := appService.accountDomainService.CreateSubAccount(ctx, mainAccountId, appId)
	if err != nil {
		return nil, err
	}
	return subAccountEntity, nil
}
